﻿@page "/Album/{id:int?}"
@using JxAudio.TransVo
@using Microsoft.Extensions.Localization
@inject NavigationManager NavigationManager
@inject HttpClient Http
@inject IStringLocalizer<Album> StringLocalizer

<PageTitle>@StringLocalizer["Title"] @_album?.Title - JxAudio</PageTitle>

<Card>
    <BodyTemplate>
        <div class="row">
            <div class="col-xs-5 col-md-3 col-sm-4 col-lg-2">
                <img alt="cover" src="/api/Cover?coverId=@_album?.CoverId" style="width: 100%;height: auto" />
            </div>
            <div class="col-md-9 col-xs-7 col-sm-8 col-lg-10">
                <div class="row">
                    <div class="col-12">
                        <h1>@_album?.Title</h1>
                    </div>
                    <div class="col-12">
                        <h3>@if (_album?.Artist == null)
                            {
                                @:未知歌手
                            }
                            else
                            {
                                @_album.Artist.Name
                            }
                        </h3>
                    </div>
                    <div class="col-1">
                        <Button Color="Color.None" style="color: red" OnClick="Star">
                            <IconPark.Like Size="18" StrokeWidth="3" Theme="@(_album?.Star == true ? IconPark.ThemeType.Filled : IconPark.ThemeType.Outline)"></IconPark.Like>
                        </Button>
                    </div>
                </div>
                 
            </div>
        </div>
    </BodyTemplate>
</Card>
<br />
<div class="g-3">
    <TrackTable Tracks="_tracks"></TrackTable>
</div>


@code {
    [Parameter]
    public int? Id { get; set; }

    private AlbumVo? _album;

    private List<TrackVo>? _tracks;


    protected override async Task OnInitializedAsync()
    {
        if (Id == null)
        {
            NavigationManager.NavigateTo("/Albums");
            return;
        }

        _album = await Http.GetFromJsonAsync<AlbumVo>($"/api/Album/?id={Id}");
        if (_album == null)
        {
            NavigationManager.NavigateTo("/Albums");
            return;
        }
        
        _tracks = await Http.GetFromJsonAsync<List<TrackVo>>($"/api/Album/AllTracks?id={Id}");
    }

    private async Task Star()
    {
        if (_album == null)
        {
            return;
        }
        
        if (_album.Star)
        {
            if (await Http.GetStringAsync("/api/Album/UnStar?id=" + _album.Id) == "s")
            {
                _album.Star = false;
            
            }
        }
        else
        {
            if (await Http.GetStringAsync("/api/Album/Star?id=" + _album.Id) == "s")
            {
                _album.Star = true;
            }
        }
    }

}